<?php
// 设置响应头
header('Content-Type: application/json');

// 检查请求方法
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => '只允许POST请求']);
    exit;
}

// 获取POST数据
$input = file_get_contents('php://input');
$data = json_decode($input, true);

// 验证数据
if (!isset($data['firstFrameImage']) || !isset($data['lastFrameImage']) || !isset($data['prompt'])) {
    http_response_code(400);
    echo json_encode(['success' => false, 'message' => '缺少必要参数']);
    exit;
}

$firstFrameImage = $data['firstFrameImage'];
$lastFrameImage = $data['lastFrameImage'];
$prompt = $data['prompt'];

// 检查系统变量
$modelscopeToken = getenv('MODELSCOPE_SDK_TOKEN');
if ($modelscopeToken === false || empty(trim($modelscopeToken))) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => '系统变量 MODELSCOPE_SDK_TOKEN 未配置']);
    exit;
}

// 调用ModelScope API
$ch = curl_init();

$apiUrl = 'https://api-inference.modelscope.cn/v1/chat/completions';

$requestData = [
    'model' => 'Qwen/Qwen3-VL-235B-A22B-Instruct',
    'messages' => [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'image_url',
                    'image_url' => [
                        'url' => $firstFrameImage
                    ]
                ],
                [
                    'type' => 'image_url',
                    'image_url' => [
                        'url' => $lastFrameImage
                    ]
                ],
                [
                    'type' => 'text',
                    'text' => $prompt
                ]
            ]
        ]
    ],
    'stream' => false
];

curl_setopt_array($ch, [
    CURLOPT_URL => $apiUrl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode($requestData),
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer ' . trim($modelscopeToken)
    ],
    CURLOPT_TIMEOUT => 90,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_USERAGENT => 'ModelScope-PHP-Client'
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);

if ($response === false || !empty($error)) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => 'API调用失败: ' . $error
    ]);
    exit;
}

if ($httpCode !== 200) {
    http_response_code($httpCode);
    echo json_encode([
        'success' => false,
        'message' => 'API返回错误状态码: ' . $httpCode,
        'response' => $response
    ]);
    exit;
}

$result = json_decode($response, true);
if ($result === null) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => 'API响应解析失败',
        'response' => $response
    ]);
    exit;
}

if (!isset($result['choices'][0]['message']['content'])) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => 'API响应格式异常',
        'response' => $result
    ]);
    exit;
}

$content = $result['choices'][0]['message']['content'];

echo json_encode([
    'success' => true,
    'content' => $content,
    'usage' => $result['usage'] ?? null
]);
?>
